package com.zygh.policeasset.listenter;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.zygh.policeasset.component.SpringJobBeanFactory;
import com.zygh.policeasset.mapper.QrCodeInfoMapper;
import com.zygh.policeasset.pojo.imports.QrCodeInfoImport;
import com.zygh.policeasset.utils.UuidUtil;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/listenter/QrCodeInfoListener.class */
public class QrCodeInfoListener extends AnalysisEventListener<QrCodeInfoImport> {
    private static final Logger log = LoggerFactory.getLogger(QrCodeInfoListener.class);
    Integer qrCodeGeneratedId;
    private static final int BATCH_COUNT = 30000;
    QrCodeInfoMapper qrCodeInfoMapper = (QrCodeInfoMapper) SpringJobBeanFactory.getBean(QrCodeInfoMapper.class);
    List<QrCodeInfoImport> list = new ArrayList();

    public QrCodeInfoListener(Integer qrCodeGeneratedId) {
        this.qrCodeGeneratedId = qrCodeGeneratedId;
    }

    public void invoke(QrCodeInfoImport data, AnalysisContext context) {
        this.list.add(checkData(data, this.qrCodeGeneratedId, Integer.valueOf(context.readRowHolder().getRowIndex().intValue() + 1)));
        if (this.list.size() >= 30000) {
            saveData();
            this.list.clear();
        }
    }

    @Override // com.alibaba.excel.read.listener.ReadListener
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        log.info("所有数据解析完成！");
    }

    private QrCodeInfoImport checkData(QrCodeInfoImport qrCodeInfoImport, Integer qrCodeGeneratedId, Integer rowIndex) {
        if (StringUtils.isEmpty(qrCodeInfoImport.getName())) {
            throw new ExcelAnalysisException("-第" + rowIndex + "行：资产名称不能为空");
        } else if (StringUtils.isEmpty(qrCodeInfoImport.getManagementUnit())) {
            throw new ExcelAnalysisException("-第" + rowIndex + "行：管理单位不能为空");
        } else if (StringUtils.isEmpty(qrCodeInfoImport.getAddress())) {
            throw new ExcelAnalysisException("-第" + rowIndex + "行：存放地址不能为空");
        } else if (StringUtils.isEmpty(qrCodeInfoImport.getUseUser())) {
            throw new ExcelAnalysisException("-第" + rowIndex + "行：使用人不能为空");
        } else {
            qrCodeInfoImport.setQrCodeGeneratedId(qrCodeGeneratedId);
            qrCodeInfoImport.setQrCode(UuidUtil.get12UUID());
            qrCodeInfoImport.setType(2);
            return qrCodeInfoImport;
        }
    }

    private void saveData() {
        log.info("{}条数据，开始存储数据库！", Integer.valueOf(this.list.size()));
        if (!CollectionUtils.isEmpty(this.list)) {
            for (int i = 0; i < this.list.size(); i++) {
                this.qrCodeInfoMapper.addQrCodeInfoImport(this.list.get(i));
            }
        }
        log.info("存储数据库成功！");
    }
}